#include "viewer.h"
#include "maskmenu.h"

MaskMenu::MaskMenu()
{
  m_Viewer = 0;

  m_tag = 1;
  m_thickness = 5;

  m_tagActive = false;

  m_number = 0;
  m_numberText = "0";
}

MaskMenu::~MaskMenu()
{
}

void
MaskMenu::activate()
{
  m_active = true;
  reset();
}

void
MaskMenu::deactivate()
{
  m_active = false;
  reset();
}

void
MaskMenu::reset()
{
  m_tagActive = false;
  m_thicknessActive = false;

  m_number = 0;
  m_numberText = "0";
}

void
MaskMenu::drawMenu()
{
  if (!m_Viewer)
    return;

  QFont tfont = QFont("Helvetica",
		      12,
		      QFont::DemiBold);
  tfont.setStyleStrategy(QFont::PreferAntialias);  

  QFontMetrics fm(tfont);
  int ht = fm.height();
  int cht = 20;
  

  int lft = 5;
  int ht0 = cht-0.7*ht;
  int ht1 = ht0 + 1.5*ht;  
  int rgt = lft + fm.width("1234567890123456");

  m_Viewer->startScreenCoordinatesSystem();

  glEnable(GL_BLEND);
  glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); // blend on top
  glColor4f(0.0f, 0.0f, 0.0f, 0.7f);
  glBegin(GL_QUADS);
  glVertex2f(lft, ht0-7);
  glVertex2f(rgt, ht0-7);
  glVertex2f(rgt, ht1+7);
  glVertex2f(lft, ht1+7);
  glEnd();
  glLineWidth(1);
  glColor4f(0.7f, 0.87f, 0.9f, 0.9f);
  glBegin(GL_LINE_STRIP);
  glVertex2f(lft, ht0-7);
  glVertex2f(rgt, ht0-7);
  glVertex2f(rgt, ht1+7);
  glVertex2f(lft, ht1+7);
  glVertex2f(lft, ht0-7);
  glEnd();


  QString str;

  //------------
  str = QString("Tag (%1)").arg(m_tag);
  m_tagRect = QRect(10, cht-ht,
		    fm.width(str), ht);

  if (m_tagActive)
    {
      glColor3f(0, 1, 0.3f);
      str = QString("Tag (%1)").arg(m_numberText);
    }
  else
    glColor3f(1,1,1);

  m_Viewer->drawText(10, cht, str, tfont);
  //------------
  
  cht += ht;

  //------------
  str = QString("Thickness (%1)").arg(m_thickness);
  m_thicknessRect = QRect(10, cht-ht,
		    fm.width(str), ht);

  if (m_thicknessActive)
    {
      glColor3f(0, 1, 0.3f);
      str = QString("Thickness (%1)").arg(m_numberText);
    }
  else
    glColor3f(1,1,1);

  m_Viewer->drawText(10, cht, str, tfont);
  //------------

  m_Viewer->stopScreenCoordinatesSystem();
}


bool
MaskMenu::checkTag(int x, int y)
{
  if (m_tagRect.contains(x, y))
    {
      m_tagActive = true;
      m_number = m_tag;
      m_numberText = QString("%1").arg(m_tag);
      return true;
    }

  m_tagActive = false;
  return false;  
}

bool
MaskMenu::checkThickness(int x, int y)
{
  if (m_thicknessRect.contains(x, y))
    {
      m_thicknessActive = true;
      m_number = m_thickness;
      m_numberText = QString("%1").arg(m_thickness);
      return true;
    }

  m_thicknessActive = false;
  return false;  
}

bool MaskMenu::menuActive() { return (m_tagActive || m_thicknessActive); }

bool
MaskMenu::processKey(QKeyEvent *event)
{
  int key = event->key();
  
  if (!menuActive())
    return false;


  if (key == Qt::Key_Return ||
      key == Qt::Key_Enter)
    {
      m_number = 0;
      m_numberText = "0";
      return true;
    }

  if (key == Qt::Key_Backspace)
    {
      m_numberText.chop(1);
      m_number = m_numberText.toFloat();

      if (m_tagActive)
	m_tag = m_number;
      else if (m_thicknessActive)
	m_thickness = m_number;

      return true;
    }

  if (key < Qt::Key_0 ||
      key > Qt::Key_9)
    return false;
  
  m_numberText += key;
  m_number = m_numberText.toFloat();


  if (m_tagActive)
    m_tag = m_number;
  else if (m_thicknessActive)
    m_thickness = m_number;

  return true;
}

